package utilities.event;

import hibernate.HibernateInitializer;
import hibernate.billStream.ServiceWrapper;
import hibernate.hibernateInterface.RoomSystemHibernateInterface;
import java.util.*;

import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class ServiceListener 
implements EventListener{
	public void onEvent(Event e){
		String action=e.getPage().getAttribute("ACTION").toString();
		if(action == null)
			return;
		else if(action.equals("FLUSH"))
			flush(e);
		else if(action.equals("ADD"))
			add(e);
		else if(action.equals("DEL"))
			del(e);
	}
	
	private void del(Event e){
		String id=e.getPage().getAttribute("ID").toString();
		if(id == null)
			try{new Messagebox().show("ID错误");}catch(Exception ee){}
		
		Session session=null;
		try{
			
			session=HibernateInitializer.getSession();
			Transaction trans=session.beginTransaction();
			
			ServiceWrapper wrapper=RoomSystemHibernateInterface.getServiceByID(new Long(id),session);
			wrapper.setCatagoriesIncluded(new HashSet());
			session.update(wrapper);
			
			trans.commit();
			session.close();
			
			session=HibernateInitializer.getSession();
			trans=session.beginTransaction();
			
			wrapper=RoomSystemHibernateInterface.getServiceByID(new Long(id),session);
			session.delete(wrapper);
			
			trans.commit();
			session.close();
			
			flush(e);
			
		}catch(Exception exp){
			try{new Messagebox().show("删除失败,该服务可能被其他房间关联");}catch(Exception exception){}
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
	}
	
	private void add(Event e){
		Session session=null;
		try{
			
			session=HibernateInitializer.getSession();
			Transaction trans=session.beginTransaction();
			
			String name=(String)e.getPage().getAttribute("NAME");
			ServiceWrapper wrapper=RoomSystemHibernateInterface.getServiceByName(name,session);
			
			name=e.getPage().getAttribute("NAME").toString();
			if(name == null || name.equals(""))
				name="null";
			String note=e.getPage().getAttribute("NOTE").toString();
			if(note == null || note.equals(""))
				note="null";
			String price=e.getPage().getAttribute("PRICE").toString();
			if(price == null || price.equals(""))
				price="null";
			
			if(wrapper == null){
				wrapper=new ServiceWrapper();
				wrapper.setCatagoriesIncluded(new HashSet());
				wrapper.setConcrete(name+"|"+note+"|"+"true|"+price+"|null");
				session.save(wrapper);
			}else{
				wrapper.setConcrete(name+"|"+note+"|"+"true|"+price+"|null");
				session.update(wrapper);
			}
			
			trans.commit();
			session.close();
			
			flush(e);
			
		}catch(Exception exp){
			try{new Messagebox().show("添加失败");}catch(Exception exception){}
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
	}
	
	private void flush(Event e){
		Session session=null;
		try{
			
			session=HibernateInitializer.getSession();
			Transaction trans=session.beginTransaction();
			
			List list=RoomSystemHibernateInterface.getAllServices(session);
			List ret=new LinkedList();
			
			Iterator itr=list.iterator();
			while(itr.hasNext()){
				ServiceWrapper wrapper=(ServiceWrapper)itr.next();
				List newCol=new LinkedList();
				ret.add(newCol);
				newCol.add(wrapper.getId());
				newCol.add(wrapper.getInner().getName());
				newCol.add(wrapper.getInner().getPricePerUnit().getFirst().toString());
				newCol.add(wrapper.getInner().getNote());
			}
			
			e.getPage().setAttribute("COLDATA",ret);
			
			trans.commit();
			session.close();
			
		}catch(Exception exp){
			exp.printStackTrace();
			try{new Messagebox().show("初始化失败");}catch(Exception exception){}
			e.getPage().setAttribute("COLDATA", new LinkedList());
		}finally{
			if(session != null && session.isOpen())
				session.close();
		}
	}
}
